var g = Graphics.createArrayBuffer(8,16,8);
g.oldSetRotation = g.setRotation;
g.setRotation = (r) => {
  g.swapped = r == 1 || r == 3;
  g.oldSetRotation(r);
  return g;
};
g.dump = _=>{
  var s = "";
  var b = new Uint8Array(g.buffer);
  var n = 0;
  var width = g.swapped ? g.getHeight() : g.getWidth();
  var height = g.swapped ? g.getWidth() : g.getHeight();
  for (var y=0;y<height;y++) {
    s+="\n";
    for (var x=0;x<width;x++)
      s+=".#"[b[n++]?1:0];
  }
  return s;
}
g.print = _=>{
  print("`"+g.dump()+"`");
}
var ok = true;
function SHOULD_BE(a) {
  var b = g.dump();
  if (a!=b) {
    console.log("GOT :"+b+"\nSHOULD BE:"+a+"\n================");
    ok = false;
  }
}

// left align
g.clear(1);
g.setClipRect(0,0, g.getWidth()-1, 0);
g.drawString("T", 0, 0); // font visibility should mean that the top line of T is drawn
g.setClipRect(0,0, g.getWidth()-1, g.getHeight()-1); // reset, just in case


SHOULD_BE(`
###.....
........
........
........
........
........
........
........
........
........
........
........
........
........
........
........`);


// Same clip rect at top, but rotate 180 degrees
g.clear(1);
g.setRotation(2);
g.setClipRect(0,0, g.getWidth()-1, 1);
g.drawString("T", 0, 0); // font visibility should mean that the top line of T is drawn
g.setClipRect(0,0, g.getWidth()-1, g.getHeight()-1); // reset, just in case

SHOULD_BE(`
........
........
........
........
........
........
........
........
........
........
........
........
........
........
......#.
.....###`);


// Test clip rect with 90/270-degree rotation
g.clear(1).setRotation(1).setClipRect(0, 0, 2, 8).drawString("T", 1, 1);
SHOULD_BE(`
........
......#.
..#####.
........
........
........
........
........
........
........
........
........
........
........
........
........`);

g.clear(1).setRotation(3).setClipRect(0, 0, 2, 8).drawString("T", 1, 1);
SHOULD_BE(`
........
........
........
........
........
........
........
........
........
........
........
........
........
.#####..
.#......
........`);


// Vector

g.clear(1).setRotation(0).drawString("HELLO",0,0);
SHOULD_BE(`
#.#.###.
#.#.#...
###.##..
#.#.#...
#.#.###.
........
........
........
........
........
........
........
........
........
........
........`);
g.clear(1).setRotation(1).drawString("HELLO",0,0).setRotation(0);
SHOULD_BE(`
...#####
.....#..
...#####
........
...#####
...#.#.#
...#...#
........
...#####
...#....
...#....
........
...#####
...#....
...#....
........`);
g.clear(1).setRotation(3).drawString("HELLO",0,0).setRotation(0);
SHOULD_BE(`
........
....#...
....#...
#####...
........
....#...
....#...
#####...
........
#...#...
#.#.#...
#####...
........
#####...
..#.....
#####...`);


result = ok;
